package cn.iocoder.yudao.module.promotion.controller.app.updateorder;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.member.api.level.MemberLevelApi;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.enums.MemberExperienceBizTypeEnum;
import cn.iocoder.yudao.module.promotion.controller.admin.paymentterm.vo.PaymentTermSaveReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.updateorder.vo.UpdateOrderPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.updateorder.vo.UpdateOrderRespVO;
import cn.iocoder.yudao.module.promotion.controller.admin.updateorder.vo.UpdateOrderSaveReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.paymentterm.PaymentTermDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.updateorder.UpdateOrderDO;
import cn.iocoder.yudao.module.promotion.service.paymentterm.PaymentTermService;
import cn.iocoder.yudao.module.promotion.service.updateorder.UpdateOrderService;
import cn.iocoder.yudao.module.trade.enums.aftersale.AfterSaleStatusEnum;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageWithdrawStatusEnum;
import cn.iocoder.yudao.module.trade.enums.delivery.DeliveryTypeEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderStatusEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 升级订单")
@RestController
@RequestMapping("/promotion/update-order")
@Validated
public class AppUpdateOrderController {

    @Resource
    private UpdateOrderService updateOrderService;

    @Resource
    private MemberLevelApi memberLevelApi;

    @Resource
    private MemberUserApi memberUserApi;

    @Resource
    private PaymentTermService paymentTermService;

    @GetMapping("/list")
    @Operation(summary = "获取团队数量", description = "获取团队数量")
    public CommonResult<List<UpdateOrderDO>> list(UpdateOrderPageReqVO pageReqVO) {
        List<UpdateOrderDO> updateOrderDOS = updateOrderService.getUpdateOrderList(pageReqVO);
        updateOrderDOS.stream().forEach(updateOrderDO -> {
            MemberUserRespDTO memberUserRespDTO = memberUserApi.getUser(Long.parseLong(updateOrderDO.getPayer()));
            updateOrderDO.setMemberUserRespDTO(memberUserRespDTO);
        });
        return success(updateOrderDOS);
    }

    @PutMapping("/update")
    @Operation(summary = "更新收款方式")
    public CommonResult<Boolean> updateUpdateOrde(@RequestBody UpdateOrderSaveReqVO updateReqVO) {
        updateOrderService.updateUpdateOrder(updateReqVO);
        if (updateReqVO.getStatus().equals("1")) {
            UpdateOrderDO updateOrderDO = updateOrderService.getUpdateOrder(updateReqVO.getId());
            memberLevelApi.addExperience(Long.parseLong(updateOrderDO.getPayer()),
                    Long.parseLong(updateOrderDO.getOrderMoney()),
                    MemberExperienceBizTypeEnum.UP_LEVEL.getType(),
                    String.valueOf(updateOrderDO.getId()));
            MemberUserRespDTO memberUserRespDTO = memberUserApi.getUser(Long.parseLong(updateOrderDO.getPayer()));
            if (memberUserRespDTO.getLevelId().compareTo(new Long(8)) == 0){
                memberUserApi.updateUserLevel(memberUserRespDTO.getId(),new Long(1), memberUserRespDTO.getExperience());
            }

            PaymentTermDO myPaymentTermDO = paymentTermService.getPaymentTermByMemberId(Long.valueOf(updateOrderDO.getMemberId()));
            PaymentTermSaveReqVO paymentTermSaveReqVO = new PaymentTermSaveReqVO();
            paymentTermSaveReqVO.setId(myPaymentTermDO.getId());
            paymentTermSaveReqVO.setOrderCount(myPaymentTermDO.getOrderCount() + 1);
            paymentTermService.updatePaymentTerm(paymentTermSaveReqVO);

            if (memberUserRespDTO.getExperience().compareTo(60L) > 0) {

                PaymentTermDO upPaymentTermDO = paymentTermService.getPaymentTermByMemberId(Long.valueOf(updateOrderDO.getPayer()));
                paymentTermSaveReqVO = new PaymentTermSaveReqVO();
                paymentTermSaveReqVO.setId(upPaymentTermDO.getId());
                paymentTermSaveReqVO.setOrderCount(0L);
                paymentTermService.updatePaymentTerm(paymentTermSaveReqVO);
            }
        }
        return success(true);
    }

    @PostMapping("/save")
    @Operation(summary = "创建升级订单")
    public CommonResult<Long> createUpdateOrder(@Valid @RequestBody UpdateOrderSaveReqVO createReqVO) {
        MemberUserRespDTO memberUserRespDTO = memberUserApi.getUser(Long.parseLong(createReqVO.getPayer()));
        if(memberUserRespDTO.getExperience().compareTo(60L) < 0) {
            createReqVO.setStatus("1");

            memberLevelApi.addExperience(Long.parseLong(createReqVO.getPayer()),
                    Long.valueOf(createReqVO.getOrderMoney()),
                    MemberExperienceBizTypeEnum.UP_LEVEL.getType(),
                    String.valueOf(createReqVO.getId()));
            if (memberUserRespDTO.getLevelId().compareTo(new Long(5)) == 0){
                memberUserApi.updateUserLevel(memberUserRespDTO.getId(),new Long(1), memberUserRespDTO.getExperience());
            }
        }

        return success(updateOrderService.createUpdateOrder(createReqVO));
    }

}